import json
import math
import os
from typing import List

from celery import Celery, platforms
from redis_connect import redis_conn

celery = Celery(__name__)
celery.conf.BROKER_URL = os.environ.get("CELERY_BROKER_URL", "redis://:123456@127.0.0.1:6379/0")
celery.conf.update(
    CELERY_ACCEPT_CONTENT=["pickle", "json"],
    CELERYD_FORCE_EXECV=True,
    CELERYD_MAX_TASKS_PER_CHILD=500,
    CELERYD_TASK_TIME_LIMIT=60,
)
platforms.C_FORCE_ROOT = True

# 垂直
vertical_radians = [
    0.0,
    -0.010157816246606997,
    -0.020507618710933373,
    -0.030892327760299633,
    -0.04153883619746504,
    -0.052883476335428184,
    -0.06291911953439558,
    -0.07229153761760514,
    -0.08491026810952414,
    -0.09367182095453566,
    -0.10515608743265836,
    -0.11449359893082801,
    -0.1265363707695889,
    -0.13625785470319732,
    -0.1457349925415265,
    -0.15699236621688992,
    -0.1747074581246324,
    -0.19167205845401725,
    -0.20956168328695915,
    -0.2255838058202671,
    -0.24443336174180585,
    -0.2617470279215896,
    -0.2792701336116127,
    -0.2964790800362767,
    -0.33182199738916196,
    -0.3638487891632579,
    -0.40280453806777133,
    -0.43554691483518493,
    -0.4895997617694493,
    -0.5376486760768532,
    -0.5916142565485178,
    -0.6383541739169261,
]
vertical_cos = [
    1.0,
    0.9999484098281471,
    0.9997897261569982,
    0.9995228699897779,
    0.9991373865891984,
    0.9986019948232421,
    0.9980212451215503,
    0.9973881045866415,
    0.9963972885204839,
    0.9956160019694038,
    0.9944761915521035,
    0.9934527647833276,
    0.992004949679715,
    0.9907312522923116,
    0.9893994377513348,
    0.9877019883166748,
    0.9847774306884783,
    0.9816870794191298,
    0.9781221922289222,
    0.9746636901644304,
    0.9702746109133566,
    0.9659393766985036,
    0.96125688501251,
    0.9563710683825496,
    0.9454503680274535,
    0.9345340659791915,
    0.9199652345995314,
    0.9066394310810253,
    0.8825211504070932,
    0.8589152094905277,
    0.830041487423207,
    0.8030775509390632,
]
vertical_sin = [
    0.0,
    -0.010157641564844264,
    -0.020506181285516727,
    -0.030887414385112246,
    -0.0415268915247301,
    -0.0528588302466262,
    -0.06287761355228416,
    -0.07222858733954823,
    -0.08480827453160218,
    -0.09353489521275012,
    -0.10496239534244589,
    -0.11424361752134074,
    -0.12619896913582976,
    -0.13583661410425352,
    -0.14521967007724038,
    -0.15634827237704724,
    -0.17382005639913703,
    -0.1905005986907628,
    -0.20803119253921376,
    -0.22367541455210288,
    -0.2420065689540977,
    -0.25876846898976397,
    -0.2756541329565838,
    -0.29215471853252795,
    -0.325766176262596,
    -0.355873684787735,
    -0.3919999580717185,
    -0.42190631899637954,
    -0.47027270714356867,
    -0.5121178213124818,
    -0.5577016488735442,
    -0.5958745230144652,
]
# 水平
horizontal_radians = [
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
    0.07330382858376185,
    0.0,
]
start_list = [20, 152, 284, 416, 548, 680, 812, 944, 1076, 1208]


def get_value_(b):
    return b & 0xFF


def get_value(b1, b2):
    return get_value_(b2) << 8 | get_value_(b1)


def parse(data, start, values):
    # 转方向角为弧度
    alpha = math.radians(get_value(data[start + 2], data[start + 3]) * 0.01)
    # 每个方向角32条线束
    for i in range(32):
        # 从方向角信息获取当前线束方向角
        delta = horizontal_radians[i]
        # 解析激光束距离
        distance = get_value(data[4 * i + start + 4], data[4 * i + start + 5]) * 4
        # 分别求 x,y,z 方向的距离
        distance_vertical_cos = distance * vertical_cos[i]
        x = round(distance_vertical_cos * math.sin(alpha + delta))
        y = round(distance_vertical_cos * math.cos(alpha + delta))
        z = abs(round(distance * vertical_sin[i]))
        if z < 4100 and (x * x + y * y) >= 25000000:
            values.append(x)
            values.append(y)
            values.append(z)


@celery.task
def parses(list_data, key):
    # 开始解析

    result: List[int] = []
    for i, value in enumerate(list_data):
        for j in range(10):
            parse(bytes.fromhex(value), j * 132 + 20, result)
    # 存储redis
    redis_conn.set(key, json.dumps(result, separators=(",", ":")), ex=60 * 3)
